package org.zyj.io.example.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.StandardCharsets;

/**
 * 不同方法对readerIndex的影响
 */
public class NettyByteBuf02 {

    public static void main(String[] args) {
        ByteBuf buf = Unpooled.copiedBuffer("hello world", StandardCharsets.UTF_8);
        if (buf.hasArray()) {
            byte[] content = buf.array();//readerIndex不变
            System.out.println(buf);
            String s1 = new String(content, StandardCharsets.UTF_8);
            System.out.println(s1);
            String s2 = buf.toString(StandardCharsets.UTF_8);
            System.out.println(s2);
            while (buf.isReadable()) { // writerIndex > readerIndex;
//                System.out.println(buf.getByte(1)); //这种方式不会改变readerIndex的值
                System.out.println(buf.readByte()); //这种方式会改变readerIndex的值
            }
        }
    }
}
